package com.mentaleye.service.impl;

import com.mentaleye.bean.GroupBean;
import com.mentaleye.dal.dao.GroupDao;
import com.mentaleye.dal.model.Group;
import com.mentaleye.dal.model.GroupExample;
import com.mentaleye.service.GroupService;
import com.mentaleye.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Rock on 2017/5/8.
 */

@Service
public class GroupServiceImpl implements GroupService{

    @Autowired private GroupDao groupDao;

    @Override
    public boolean edit(GroupBean bean) {
        Group modle = (Group)ObjectUtil.transfer(bean, Group.class);
        boolean success;
        if (bean.getGroupId() != null){
            success = groupDao.update(modle);
        }else {
            success = groupDao.insert(modle);
            if(success){
                Group group = new Group();
                group.setGroupId(modle.getGroupId());
                group.setGroupUrl(bean.getGroupUrl() + modle.getGroupId() + ";");
                success = groupDao.update(group);
            }
        }
        return success;
    }

    @Override
    public boolean remove(Integer groupId) {
        return groupDao.delete(groupId);
    }

    @Override
    public boolean removeByParentId(Integer parentId) {
        GroupExample example = new GroupExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return groupDao.deleteByExample(example);
    }

    @Override
    public GroupBean query(Integer groupId) {
        return (GroupBean) ObjectUtil.transfer(groupDao.select(groupId),GroupBean.class);
    }

    @Override
    public List<GroupBean> queryByParentId(Integer parentId) {
        GroupExample example = new GroupExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return (List<GroupBean>) ObjectUtil.transferList(groupDao.selectByExample(example), GroupBean.class);
    }

    @Override
    public List<GroupBean> queryList(Integer groupType) {
        GroupExample example = new GroupExample();
        example.createCriteria().andGroupTypeEqualTo(groupType);
        return (List<GroupBean>)ObjectUtil.transferList(groupDao.selectByExample(example), GroupBean.class);
    }
}
